package io.school;

import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author haomin
 * @date 2022/06/15 15:28
 **/
public class Exercise12_20 {
    public static void main(String [] args) throws Exception
    {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入文件的路径");
        String next = scanner.next();


        File initialFile = new File(next);
        if (!initialFile.exists())
        {
            System.out.println(next+ " is nnot exist");
            System.exit(2);
        }

        deleteWords(initialFile);
    }

    public static void deleteWords(File file) throws Exception
    {
        if (file.isFile())
        {
            delete(file);
        }
        else
        {
            File[] fileList = file.listFiles();
            for (File everyFile : fileList)
            {
                deleteWords(everyFile);
            }
        }
    }

    public static void delete(File file) throws Exception
    {
        ArrayList<String> list = new ArrayList<>();
        Scanner input = new Scanner(file);
        while (input.hasNext())
        {
            list.add(input.nextLine());
        }

        if (list.get(0).equals("//" + "package chapteri"))
        {
            System.out.println("Has " + file.getParent());
            list.remove(0);
        }

        //print(list);
        try(
                PrintWriter output = new PrintWriter(file);
        ){
            for (String everyLine : list)
            {
                output.println(everyLine);
            }
        }
        //print(list);
    }

    public static void print(ArrayList<String> list)
    {
        for (String str : list)
        {
            System.out.println(str);
        }
    }

}